package com.kim.dingding.robotsend;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.kim.dingding.model.DingdingModel;
import com.kim.dingding.model.vo.SignatureVO;
import com.kim.dingding.signature.Signature;

import java.util.Arrays;

/**
 * @author KimWu
 * @date 2020/6/16 23:40
 * @Description ：
 */
public class Robot {

    public static void robotSend(String textStr) {
        try {
            SignatureVO sign = Signature.sign();
            String url = DingdingModel.webhook +
                    "&timestamp=" + sign.getTimestamp() + "&sign=" + sign.getSign();
            System.out.println("url = " + url);
            DingTalkClient client = new DefaultDingTalkClient(url);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype(DingdingModel.msgType);
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent(textStr);
            request.setText(text);
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setAtMobiles(Arrays.asList(DingdingModel.mobile)); //被@人的手机号（在content里添加@人的手机号）
            // isAtAll类型如果不为Boolean，请升级至最新SDK
            at.setIsAtAll(true);
            request.setAt(at);

       /* request.setMsgtype("link");
        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setMessageUrl("https://www.dingtalk.com/");
        link.setPicUrl("");
        link.setTitle("时代的火车向前开");
        link.setText("这个即将发布的新版本，创始人xx称它为红树林。而在此之前，每当面临重大升级，产品经理们都会取一个应景的代号，这一次，为什么是红树林");
        request.setLink(link);

        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("杭州天气");
        markdown.setText("#### 杭州天气 @156xxxx8827\n" +
                "> 9度，西北风1级，空气良89，相对温度73%\n\n" +
                "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" +
                "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
        request.setMarkdown(markdown);*/
            OapiRobotSendResponse response = client.execute(request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("钉钉机器人发送消息失败");
        }
    }
}
